package com.aoxin.service.impl.opm;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.course.webCourse.SlfClassMapper;
import com.aoxin.mapper.opm.SlfEmployeeMapper;
import com.aoxin.mapper.opm.SlfRoleEmployeeMapper;
import com.aoxin.model.dto.SelectDto;
import com.aoxin.model.opm.SlfEmployee;
import com.aoxin.model.opm.SlfRoleEmployee;
import com.aoxin.service.inter.opm.EmployeeService;
import com.aoxin.util.AESUtil;
import com.aoxin.util.CommonUtil;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.OSSUtils;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.md5.MD5;
import com.aoxin.util.md5.Md5Util;
import com.aoxin.util.qrcode.QRCode;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private SlfEmployeeMapper slfEmployeeMapper;
    @Autowired
    private SlfRoleEmployeeMapper slfRoleEmployeeMapper;

    @Autowired
    private SlfClassMapper slfClassMapper;

    @Override
    @Transactional
    public int insertEmployee(SlfEmployee employee, Integer roleId) throws Exception {
        Assert.notNull(employee, String.format(ErrorUtil.PARAMETER_NULL, "employee"));
        if (null == employee.getSequence()) {
            employee.setSequence(this.slfEmployeeMapper.getNextSequence(employee.getSchoolId()).intValue());
        }
        if (this.slfEmployeeMapper.checkLoginName(employee.getLoginAccount(), null) > 0L) {
            throw new IllegalArgumentException(String.format(ErrorUtil.HAS_LOGIN_ACCOUNT, employee.getLoginAccount()));
        }
        // 设置默认密码
        employee.setLoginPsd(Md5Util.md5(ErrorUtil.DEFAULT_PASSWORD));
        this.slfEmployeeMapper.insertSelective(employee);

        //生成二维码
        QRCode qrCode = new QRCode();
        String imgPath = CommonUtil.getFilePath() + File.separator + employee.getName() + employee.getEmployeeId() + ".png";

        qrCode.encoderQRCode(AESUtil.encrypt(employee.getEmployeeId().toString(), AESUtil.PASSWORD), imgPath, "png", 9);

        // 上传值OSS
        File file = new File(imgPath);
        String path = OSSUtils.upload(file);
        file.delete();
        employee.setQrCode(path);
        this.slfEmployeeMapper.updateByPrimaryKeySelective(employee);

        // 为员工添加角色
        SlfRoleEmployee slfRoleEmployee = new SlfRoleEmployee();
        slfRoleEmployee.setEmployeeId(employee.getEmployeeId());
        slfRoleEmployee.setRoleId(roleId);
        this.slfRoleEmployeeMapper.insertSelective(slfRoleEmployee);


        return employee.getEmployeeId();
    }

    @Override
    @Transactional
    public void deleteEmployeeByIds(List<Integer> employeeIds) {
        Assert.notEmpty(employeeIds, String.format(ErrorUtil.PARAMETER_NULL, "employeeIds"));
        // TODO 删除人员时逻辑判断
        // for (Integer integer : employeeIds) {
        // }
        this.slfEmployeeMapper.deleteByPrimaryKeys(employeeIds);
    }

    @Override
    @Transactional
    public void updateEmployee(com.aoxin.model.opm.SlfEmployee employee) {
        Assert.notNull(employee, String.format(ErrorUtil.PARAMETER_NULL, "employee"));
        if (this.slfEmployeeMapper.checkLoginName(employee.getLoginAccount(), employee.getEmployeeId()) > 0L) {
            throw new IllegalArgumentException(String.format(ErrorUtil.HAS_LOGIN_ACCOUNT, employee.getLoginAccount()));
        }
        this.slfEmployeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public SlfEmployee getEmployee(Integer employeeId) {
        Assert.notNull(employeeId, String.format(ErrorUtil.PARAMETER_NULL, "employeeId"));
        return this.slfEmployeeMapper.selectByPrimaryKey(employeeId);
    }

    @Override
    public List<SlfEmployee> getEmployeeList(Map<String, Object> map) {
        if (map.get("state") == null || "".equals(map.get("state"))) {
            map.put("state", 1);
        }
        List<SlfEmployee> emplyList = this.slfEmployeeMapper.getList(map);
        for (int i = 0, b = emplyList.size(); i < b; i++) {
            List<Map<String, Object>> roleNameAndId = slfEmployeeMapper.queryRoleNameAndId(emplyList.get(i).getEmployeeId());
            emplyList.get(i).setRoleId(Integer.parseInt(roleNameAndId.get(0).get("roleId").toString()));
            emplyList.get(i).setRoleName(roleNameAndId.get(0).get("roleName").toString());

        }
        return emplyList;

    }

    @Override
    @Transactional
    public void updateStates(List<Integer> employeeIds, Integer state) {
        Assert.notEmpty(employeeIds, String.format(ErrorUtil.PARAMETER_NULL, "employeeIds"));
        Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "state"));
        this.slfEmployeeMapper.updateStates(employeeIds, state);
    }

    @Override
    public SlfEmployee getByLoginNameAndPassword(String loginName, String password) {
        Assert.hasText(loginName, ErrorUtil.LOGIN_ACCOUNT_NULL);
        Assert.hasText(password, ErrorUtil.PARAMETER_NULL);
        SlfEmployee employee = this.slfEmployeeMapper.getByLoginNameAndPassword(loginName, Md5Util.md5(password));
        // TODO 查询员工角色

        return employee;
    }

    @Override
    @Transactional
    public void iniPassword(List<Integer> employeeIds, String oldPassword, String newPassword, Integer type) {
        Assert.notEmpty(employeeIds, String.format(ErrorUtil.PARAMETER_NULL, "employeeIds"));
        Assert.notNull(type, String.format(ErrorUtil.PARAMETER_NULL, "type"));
        if (type == 0) {
            String password = MD5.md5("123456");
            this.slfEmployeeMapper.iniPassword(employeeIds, password);
        } else {
            Assert.hasText(oldPassword, ErrorUtil.LOGIN_ACCOUNT_NULL);
            Assert.hasText(newPassword, ErrorUtil.LOGIN_ACCOUNT_NULL);
            Integer result = 0;
            for (Integer employeeId : employeeIds) {
                String password = MD5.md5(oldPassword);
                result = this.slfEmployeeMapper.getCount(employeeId, password);
            }
            if (result != 0) {
                String password = MD5.md5(newPassword);
                this.slfEmployeeMapper.iniPassword(employeeIds, password);
            }
        }

    }

    @Override
    public List<SelectDto> getEmployeeListByOrg() {
        return this.slfEmployeeMapper.getListByOrg(ThreadLocalUtil.getEmployee().getOrgId(), ThreadLocalUtil.getEmployee().getSchoolId());
    }

    @Override
    public List<SelectDto> getTeacherBySchoolId(Integer schoolId) {
        Assert.notNull(schoolId, String.format(ErrorUtil.PARAMETER_NULL, "schoolId"));
        return this.slfEmployeeMapper.getTeacherBySchoolId(schoolId);
    }
}
